<template>
  <div>
    <!-- 标题 -->
    <van-nav-bar
      :title="$route.name"
      @click-left="$router.back()"
      left-arrow
      right-text="注册"
      @click-right="$router.push('/register')"
    />
     <!-- 图片logo -->
    <div class="logo">
      <img class="img" :src="require('../assets/images/logo/orange.png')"/>
    </div>
    <!-- 输入表单 -->
    <van-form @submit="login">
      <van-field
        v-model="userlist.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请输入手机号' }]"
      />
      <van-field
        v-model="userlist.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from "../request/api";
import { Toast } from "vant";
export default {
  data() {
    return {
      userlist: {
        phone: "",
        password: ""
      }
    };
  },
  methods: {
    //封装一个注册事件
    login() {
      login(this.userlist).then(res => {
        if (res.code == 200) {
          this.$store.dispatch("changeUser", res.list);
          Toast.success(res.msg);
          this.$router.push("/home");
        } else {
          Toast.fail(res.msg);
        }
      });
    }
  }
};
</script>

<style scoped>
.van-nav-bar {
  background: #ff6040;
  height: 50px;
  color: #fff;
}
.logo{
  width: 100%;
  text-align: center;
}
.img{
  width: 50%;
  margin: 100px 0 10px 0;
}
.van-form{
  margin-left: 10px;
}
.van-button{
  background: #ff6040;
  border: none
}
</style>
